package yf.mall.service.impl;

import com.yf.response.RRException;
import com.yf.service.impl.BaseServiceImpl;
import yf.mall.dao.*;
import yf.mall.entity.*;
import yf.mall.enums.MallActStatusEnum;
import yf.mall.enums.MallGoodsSaleTypeEnum;
import yf.mall.service.CartService;
import yf.mall.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

/**
 * 购物车Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Fri Aug 23 11:38:38 CST 2019
 */
@Service
public class CartServiceImpl extends BaseServiceImpl<CartEntity> implements CartService {

    private CartDao cartDao;


    @Autowired
    public void setDao(CartDao cartDao) {
        this.cartDao = cartDao;
        super.setBaseDao(cartDao);
    }


    /**
     * 内部方法
     **/

    /**
     * 增加购物车
     *
     * @param cartEntity
     * @param amount
     * @return
     */
    private CartEntity addCart(CartEntity cartEntity, Integer amount) {
        cartEntity.setQuantity(cartEntity.getQuantity() + amount);
        return cartEntity;
    }

    /**
     * 减少购物车
     *
     * @param cartEntity
     * @param amount
     * @return
     */
    private CartEntity decreaseCart(CartEntity cartEntity, Integer amount) {
        Integer tem = cartEntity.getQuantity() - amount;
        if (tem > 0) {
            cartEntity.setQuantity(tem);
        } else {//如果小于0 移除
            cartDao.delete(cartEntity.getId());
        }
        return cartEntity;
    }

    /**
     * 外部方法实现
     **/

    @Override
    public List<CartVo> findByUserId(Long userId) {
        return cartDao.findByUserId(userId);
    }

    @Autowired
    private MallSkuDao mallSkuDao;

    @Autowired
    private MallGoodsDao mallGoodsDao;

    @Autowired
    private StoresDao storesDao;

    @Autowired
    private LevelDao levelDao;
    @Autowired
    private MallActDao actDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCart(Long userId, Long goodsId, String skuName, Integer quantity) {
        MallSkuEntity mallSkuEntity = this.mallSkuDao.queryNameAndGoodsId(skuName, goodsId);
        if (mallSkuEntity != null && mallSkuEntity.getDisable()) {
            throw new RRException("该规格已禁用");
        }

            MallGoodsEntity mallGoodsEntity = mallGoodsDao.get(goodsId);
        if (mallGoodsEntity==null){
            return;
        }
        CartEntity cartEntity = cartDao.findByUserIdAndGoodsIdWidthSkuName(userId, goodsId, skuName);
        if (cartEntity == null) {
                //创建一个购物车子集
                cartEntity = new CartEntity(userId, goodsId, skuName, quantity == null ? 0 : quantity, true);
                cartDao.save(cartEntity);
        } else {//如果存在 数量加1 并更新
            if (mallGoodsEntity.getSaleType().equals(MallGoodsSaleTypeEnum.LOTTERY.getIndex())){
                cartEntity.setQuantity(1);
                cartDao.update(cartEntity);
            }else {
            addCart(cartEntity, quantity);
            cartDao.update(cartEntity);
        }
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void change(Long userId, Long cartId, Integer amount, Boolean selected,Long bindId) {
        CartEntity cartEntity = cartDao.get(cartId);

        if (cartEntity == null) {
            throw new RRException("购物车产品不存在!");
        }
        if (!cartEntity.getUserId().equals(userId)) {
            throw new RRException("非法操作购物车!");
        }
        if (amount > 0) {
            cartEntity.setQuantity(amount);
            cartEntity.setSelected(selected);
            //如果存在绑定产品 主产品选择绑定产品也要对应选择
//            if (bindId != null){
//                MallActEntity act = actDao.findByGoodsIdAndstatus(cartEntity.getGoodsId(), MallActStatusEnum.OPEN.getIndex());
//                if(act != null && act.getBindGoodsId() != null){
//                    CartEntity cart = cartDao.get(bindId);
//                    if (amount%act.getFirstGoodsQuantity() == 0){
//                        cart.setQuantity(amount/ act.getFirstGoodsQuantity() * act.getBindGoodsQuantity());
//                    }
//                    cart.setSelected(selected);
//                    cartDao.update(cart);
//                }
//            }
            cartDao.update(cartEntity);
        }

    }

    @Override
    public List<CartEntity> findByUserIdAndCartIds(Long userId, List<Long> cartIds) {
        if (cartIds == null || cartIds.isEmpty()) {
            return new ArrayList<>();
        }
        List<CartEntity> carts = cartDao.findByUserIdAndCartIds(userId, cartIds);
        return carts;
    }

    @Override
    public List<CartEntity> findByUserIdWithSelected(Long userId) {
        return cartDao.findByUserIdWithSelected(userId);
    }

    @Override
    public void changeAllSelected(Long userId, boolean changeAllSelected) {
        cartDao.changeAllSelectedByUser(userId, changeAllSelected);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSelected(Long userId) {
        cartDao.deleteSelected(userId);
    }

    @Override  //清空购物车
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(long userId) {
        cartDao.deleteAll(userId);
    }
}
